import store from '@/store'
import router from '@/router'

const addRouteFun = (tree,views,childrens)=>{
    tree.map((node)=>{
        const tempNode = node;
        if(tempNode.component){
            tempNode.component = views[`../${tempNode.component}`];
            childrens.push(
                {
                    name:tempNode.route_name,
                    path:'/'+tempNode.route_name,
                    component:tempNode.component
                },
            );
        }
        if(tempNode.children && tempNode.children.length > 0){
            addRouteFun(tempNode.children,views,childrens);
        }
    })
}

const addRouters = ()=>{
    if(store.state.menuList && store.state.menuList.length > 0){
        const menuData = JSON.parse(JSON.stringify(store.state.menuList));
        const views = import.meta.glob('../views/**/*.vue');
        let fatherRoute = {
            name:'layout',
            path:'/layout',
            component:()=>import('@/layout/storm/Index.vue'),
        };
        let childrens = [
            {
                name:'home',
                path:'/home',
                component:()=>import('@/views/Home/Index.vue'),
            }
        ];
        addRouteFun(menuData,views,childrens);
        fatherRoute.children = childrens;
        router.addRoute(fatherRoute);
        //添加404页面
        router.addRoute({
            path:'/:pathMatch(.*)*',
            name:'404',
            component:()=>import('@/views/Empty.vue'),
        });
    }
}

const clearRouters = ()=>{

}

export{ addRouters, clearRouters}